﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TLang.Values
{
    using Ast;

    public class RecordType : Value
    {
        public String name;
        public Node definition;
        public Scope properties;


        public RecordType(String name, Node definition, Scope properties)
        {
            this.name = name;
            this.definition = definition;
            this.properties = properties.Copy();
        }


        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Constants.PAREN_BEGIN);
            sb.Append(Constants.RECORD_KEYWORD).Append(" ");
            sb.Append(name == null ? "_" : name);

            foreach (string field in properties.Keys)
            {
                sb.Append(" ").Append(Constants.SQUARE_BEGIN);
                sb.Append(field);

                Dictionary<string, object> m = properties.LookupAllProps(field);
                foreach (string key in m.Keys)
                {
                    m.TryGetValue(key, out object value);
                    if (value != null)
                    {
                        sb.Append(" :" + key + " " + value);
                    }
                }
                sb.Append(Constants.SQUARE_END);
            }

            sb.Append(Constants.PAREN_END);

            return sb.ToString();
        }
    }
}
